import 'package:flutter/material.dart';

import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

import '../../../presenter/state_chapter_detail_presenter.dart';
import 'novel_detail_page.dart';

class ChapterDetailPage extends StatefulWidget {
  static const routeName = '${NovelDetailPage.routeName}/chapter_detail';
  const ChapterDetailPage(
      {Key? key, required this.presenter, required this.controller})
      : super(key: key);
  final StateChapterDetailPresenter presenter;
  final ChapterDetailController controller;

  @override
  State<ChapterDetailPage> createState() => _ChapterDetailPageState();
}

class _ChapterDetailPageState extends State<ChapterDetailPage> {
  late final ScrollController _controller;

  @override
  void initState() {
    _controller = ScrollController();
    widget.presenter.presenterContext = context;
    widget.presenter.presenterSetState = setState;
    Future.delayed(Duration.zero)
        .then((value) => widget.presenter.chapter =
            ModalRoute.of(context)?.settings.arguments as Chapter)
        .then((value) => widget.controller.getChapterContent())
        .then((value) {
      _controller.jumpTo(
        widget.presenter.offset,
      );
      final defaultFontSize =
          Theme.of(context).primaryTextTheme.titleSmall!.fontSize!;
      widget.presenter.fontSize = widget.presenter.fontSize == 0
          ? defaultFontSize
          : widget.presenter.fontSize;
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.presenter.chapter?.title ?? I18n.loading),
        actions: [
          TextButton(
            onPressed: () {
              setState(() {
                widget.presenter.fontSize -= 2.0;
                widget.presenter.fontSize = widget.presenter.fontSize.clamp(
                    Theme.of(context).primaryTextTheme.titleSmall!.fontSize!,
                    Theme.of(context).primaryTextTheme.titleLarge!.fontSize!);
              });
            },
            child: Text(
              I18n.small,
              style: TextStyle(
                  color: Theme.of(context).secondaryHeaderColor,
                  fontSize:
                      Theme.of(context).primaryTextTheme.titleSmall!.fontSize),
            ),
          ),
          TextButton(
              onPressed: () {
                setState(() {
                  widget.presenter.fontSize += 2.0;
                  widget.presenter.fontSize = widget.presenter.fontSize.clamp(
                      Theme.of(context).primaryTextTheme.titleSmall!.fontSize!,
                      Theme.of(context).primaryTextTheme.titleLarge!.fontSize!);
                });
              },
              child: Text(
                I18n.large,
                style: TextStyle(
                    color: Theme.of(context).secondaryHeaderColor,
                    fontSize: Theme.of(context)
                        .primaryTextTheme
                        .titleLarge!
                        .fontSize),
              ))
        ],
      ),
      body: SingleChildScrollView(
        controller: _controller,
        child: Text(
          widget.presenter.chapter?.content ?? "",
          style: TextStyle(fontSize: widget.presenter.fontSize),
        ),
      ),
    );
  }
}
